

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="caoyusang">
  <meta name="keywords" content="">
  <title>JD笔试 - caoyusang的科研日常</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>caoyusang</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/7.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-03 15:51" pubdate>
        2021年5月3日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.8k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      62
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">JD笔试</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：25 天前
                
              </p>
            
            <div class="markdown-body" id="post-body">
              <h2 id="京东笔试"><a href="#京东笔试" class="headerlink" title="京东笔试"></a>京东笔试</h2><p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/reverse-linked-list/">206-反转链表</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">reverseList</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        ListNode pre = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        pre.next = head;
        ListNode p = head;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span> &amp;&amp; p.next != <span class="hljs-keyword">null</span>)&#123;
            ListNode temp = pre.next;
            pre.next = p.next;
            p.next = p.next.next;
            pre.next.next = temp;
        &#125;
        <span class="hljs-keyword">return</span> pre.next;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/reverse-linked-list-ii/">92-反转链表Ⅱ</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">reverseBetween</span><span class="hljs-params">(ListNode head, <span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> right)</span> </span>&#123;
        ListNode res = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        res.next = head;
        ListNode p = head, pre = res;
        <span class="hljs-keyword">int</span> index = <span class="hljs-number">1</span>;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span> &amp;&amp; p.next != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (index &lt; left)&#123;
                <span class="hljs-comment">//pre和p同步移动,位置index加1</span>
                pre = pre.next;
                p = p.next;
                index++;
            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (index &gt;= right)&#123;
                <span class="hljs-comment">//pre不用移动了，位置index也不用加了</span>
                p = p.next;
            &#125; <span class="hljs-keyword">else</span> &#123;
                <span class="hljs-comment">//需要翻转的部分</span>
                ListNode temp = pre.next;
                pre.next = p.next;
                p.next = p.next.next;
                pre.next.next = temp;
                index++;
            &#125;
        &#125;
        <span class="hljs-keyword">return</span> res.next;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/reverse-nodes-in-k-group/">25-K个一组翻转链表</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">reverseKGroup</span><span class="hljs-params">(ListNode head, <span class="hljs-keyword">int</span> k)</span> </span>&#123;
        ListNode res = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        res.next = head;
        ListNode pre = res, p = head;
        <span class="hljs-comment">//统计总结点数</span>
        <span class="hljs-keyword">int</span> nodeNum = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span>)&#123;
            p = p.next;
            nodeNum++;
        &#125;

        <span class="hljs-comment">//计算分组数</span>
        <span class="hljs-keyword">int</span> groupNum = nodeNum / k;
        
        p = head;
        <span class="hljs-comment">//K个一组翻转链表</span>
        <span class="hljs-keyword">while</span> (groupNum-- &gt; <span class="hljs-number">0</span>)&#123;
            <span class="hljs-comment">//统计翻转次数，两个一组(k == 2)进行翻转，只需要翻转一次</span>
            <span class="hljs-keyword">int</span> cnt = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">while</span> (p != <span class="hljs-keyword">null</span> &amp;&amp; p.next != <span class="hljs-keyword">null</span> &amp;&amp; ++cnt &lt; k)&#123;
                ListNode temp = pre.next;
                pre.next = p.next;
                p.next = p.next.next;
                pre.next.next = temp;
            &#125;
            <span class="hljs-comment">//准备下一组翻转/剩余部分不翻转</span>
            pre = p;
            p = p.next;
        &#125;
        <span class="hljs-keyword">return</span> res.next;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/linked-list-cycle/">141-环形链表</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode(int x) &#123;</span>
<span class="hljs-comment"> *         val = x;</span>
<span class="hljs-comment"> *         next = null;</span>
<span class="hljs-comment"> *     &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">hasCycle</span><span class="hljs-params">(ListNode head)</span> </span>&#123;
        ListNode fastPtr = head, slowPtr = head;
        <span class="hljs-keyword">while</span> (fastPtr != <span class="hljs-keyword">null</span> &amp;&amp; fastPtr.next != <span class="hljs-keyword">null</span>)&#123;
            fastPtr = fastPtr.next.next;
            slowPtr = slowPtr.next;
            <span class="hljs-keyword">if</span> (fastPtr == slowPtr)&#123;
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            &#125;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/linked-list-cycle-ii/">142-环形链表Ⅱ</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode(int x) &#123;</span>
<span class="hljs-comment"> *         val = x;</span>
<span class="hljs-comment"> *         next = null;</span>
<span class="hljs-comment"> *     &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">detectCycle</span><span class="hljs-params">(ListNode head)</span> </span>&#123;

        <span class="hljs-comment">//快慢指针</span>
        ListNode slowPtr = head, fastPtr = head;
        <span class="hljs-keyword">while</span> (fastPtr != <span class="hljs-keyword">null</span> &amp;&amp; fastPtr.next != <span class="hljs-keyword">null</span>)&#123;
            slowPtr = slowPtr.next;
            fastPtr = fastPtr.next.next;
            <span class="hljs-comment">//第一次相遇，这时候，快指针比慢指针已经多走了链表环的长度</span>
            <span class="hljs-keyword">if</span> (fastPtr == slowPtr) <span class="hljs-keyword">break</span>;
        &#125;
		<span class="hljs-comment">//无环情况</span>
        <span class="hljs-keyword">if</span> (fastPtr == <span class="hljs-keyword">null</span> || fastPtr.next == <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        &#125;
        <span class="hljs-comment">//慢指针从头开始，快指针模仿慢指针继续每次走一步，两方相遇点即为环的入口节点</span>
        slowPtr = head;
        <span class="hljs-comment">//第二次相遇即环的入口节点</span>
        <span class="hljs-keyword">while</span> (slowPtr != fastPtr)&#123;
            slowPtr = slowPtr.next;
            fastPtr = fastPtr.next;
        &#125;
        <span class="hljs-keyword">return</span> slowPtr;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/binary-tree-level-order-traversal/">102-二叉树的层序遍历</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for a binary tree node.</span>
<span class="hljs-comment"> * public class TreeNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     TreeNode left;</span>
<span class="hljs-comment"> *     TreeNode right;</span>
<span class="hljs-comment"> *     TreeNode() &#123;&#125;</span>
<span class="hljs-comment"> *     TreeNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     TreeNode(int val, TreeNode left, TreeNode right) &#123;</span>
<span class="hljs-comment"> *         this.val = val;</span>
<span class="hljs-comment"> *         this.left = left;</span>
<span class="hljs-comment"> *         this.right = right;</span>
<span class="hljs-comment"> *     &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    List&lt;List&lt;Integer&gt;&gt; res = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
    <span class="hljs-keyword">public</span> List&lt;List&lt;Integer&gt;&gt; levelOrder(TreeNode root) &#123;
        <span class="hljs-keyword">if</span> (root == <span class="hljs-keyword">null</span>) <span class="hljs-keyword">return</span> res;
        Deque&lt;TreeNode&gt; q = <span class="hljs-keyword">new</span> ArrayDeque&lt;&gt;();
        q.offer(root);
        <span class="hljs-keyword">while</span> (!q.isEmpty())&#123;
            <span class="hljs-keyword">int</span> size = q.size();
            List&lt;Integer&gt; temp = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; size; i++)&#123;
                TreeNode cur = q.poll();
                temp.add(cur.val);
                <span class="hljs-keyword">if</span> (cur.left != <span class="hljs-keyword">null</span>) q.offer(cur.left);
                <span class="hljs-keyword">if</span> (cur.right != <span class="hljs-keyword">null</span>) q.offer(cur.right);
            &#125;
            res.add(temp);
        &#125;
        <span class="hljs-keyword">return</span> res;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/binary-tree-inorder-traversal/">94-二叉树的中序遍历</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for a binary tree node.</span>
<span class="hljs-comment"> * public class TreeNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     TreeNode left;</span>
<span class="hljs-comment"> *     TreeNode right;</span>
<span class="hljs-comment"> *     TreeNode() &#123;&#125;</span>
<span class="hljs-comment"> *     TreeNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     TreeNode(int val, TreeNode left, TreeNode right) &#123;</span>
<span class="hljs-comment"> *         this.val = val;</span>
<span class="hljs-comment"> *         this.left = left;</span>
<span class="hljs-comment"> *         this.right = right;</span>
<span class="hljs-comment"> *     &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;Integer&gt; <span class="hljs-title">inorderTraversal</span><span class="hljs-params">(TreeNode root)</span> </span>&#123;
        helper(root);
        <span class="hljs-keyword">return</span> res;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">helper</span><span class="hljs-params">(TreeNode node)</span></span>&#123;
        <span class="hljs-keyword">if</span> (node == <span class="hljs-keyword">null</span>) <span class="hljs-keyword">return</span>;
        helper(node.left);
        res.add(node.val);
        helper(node.right);
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/">剑指Offer 10-Ⅱ.青蛙跳台阶问题</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">numWays</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (n &lt;= <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
        <span class="hljs-comment">// int[] dp = new int[n + 1];</span>
        <span class="hljs-comment">// dp[0] = 1;</span>
        <span class="hljs-comment">// dp[1] = 1;</span>
        <span class="hljs-comment">// for (int i = 2; i &lt;= n; i++)&#123;</span>
        <span class="hljs-comment">//     dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007;</span>
        <span class="hljs-comment">// &#125;</span>
        <span class="hljs-comment">// return dp[n] % 1000000007;</span>

        <span class="hljs-keyword">int</span> dp_1 = <span class="hljs-number">1</span>, dp_2 = <span class="hljs-number">2</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt; n; i++)&#123;
            <span class="hljs-keyword">int</span> temp = (dp_1 + dp_2) % <span class="hljs-number">1000000007</span>;
            dp_1 = dp_2;
            dp_2 = temp;
        &#125;
        <span class="hljs-keyword">return</span> dp_2 % <span class="hljs-number">1000000007</span>;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/combination-sum/">39-组合总和</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    List&lt;List&lt;Integer&gt;&gt; res = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
    <span class="hljs-keyword">public</span> List&lt;List&lt;Integer&gt;&gt; combinationSum(<span class="hljs-keyword">int</span>[] candidates, <span class="hljs-keyword">int</span> target) &#123;
        <span class="hljs-keyword">if</span> (candidates == <span class="hljs-keyword">null</span> || candidates.length == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> res;
        backTrack(candidates, <span class="hljs-keyword">new</span> ArrayList&lt;Integer&gt;(), <span class="hljs-number">0</span>, target);
        <span class="hljs-keyword">return</span> res;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">backTrack</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums, List&lt;Integer&gt; temp, <span class="hljs-keyword">int</span> index, <span class="hljs-keyword">int</span> k)</span></span>&#123;
        <span class="hljs-comment">//终止条件</span>
        <span class="hljs-keyword">if</span> (k &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span>;
        <span class="hljs-keyword">if</span> (k == <span class="hljs-number">0</span>)&#123;
            res.add(<span class="hljs-keyword">new</span> ArrayList&lt;&gt;(temp));
            <span class="hljs-keyword">return</span>;
        &#125;

        <span class="hljs-comment">//从nums[index]开始进行组合，把含数nums[0 ~ index - 1]的组合全部排除，防止重复组合</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = index; i &lt; nums.length; i++)&#123;
            <span class="hljs-comment">//累积和还没达到target,才需要继续组合</span>
            <span class="hljs-keyword">if</span> (k &gt; <span class="hljs-number">0</span>)&#123;
                <span class="hljs-comment">//做选择</span>
                temp.add(nums[i]);

                <span class="hljs-comment">//回溯</span>
                backTrack(nums, temp, i, k - nums[i]);

                <span class="hljs-comment">//撤销选择</span>
                temp.remove(temp.size() - <span class="hljs-number">1</span>);
            &#125;
            
        &#125;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/two-sum/">1-两数之和</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span>[] twoSum(<span class="hljs-keyword">int</span>[] nums, <span class="hljs-keyword">int</span> target) &#123;
        <span class="hljs-keyword">int</span> n = nums.length;
        Map&lt;Integer, Integer&gt; map = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++)&#123;
            <span class="hljs-keyword">if</span> (map.containsKey(nums[i]))&#123;
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[]&#123;i, map.get(nums[i])&#125;;
            &#125; <span class="hljs-keyword">else</span> &#123;
                map.put(target - nums[i], i);
            &#125;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">2</span>];
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/binary-search/">704-二分查找</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">search</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums, <span class="hljs-keyword">int</span> target)</span> </span>&#123;
        <span class="hljs-keyword">int</span> leftPtr = <span class="hljs-number">0</span>, rightPtr = nums.length - <span class="hljs-number">1</span>;
        <span class="hljs-keyword">while</span> (leftPtr &lt;= rightPtr)&#123;
            <span class="hljs-keyword">int</span> mid = (leftPtr + rightPtr) / <span class="hljs-number">2</span>;
            <span class="hljs-keyword">if</span> (nums[mid] &gt; target)&#123;
                rightPtr = mid - <span class="hljs-number">1</span>;
            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (nums[mid] &lt; target)&#123;
                leftPtr = mid + <span class="hljs-number">1</span>;
            &#125; <span class="hljs-keyword">else</span> &#123;
                <span class="hljs-keyword">return</span> mid; 
            &#125;

        &#125;
        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/">剑指Offer 25.合并两个排序的链表</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode(int x) &#123; val = x; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeTwoLists</span><span class="hljs-params">(ListNode l1, ListNode l2)</span> </span>&#123;
        ListNode res = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        ListNode pre = res;
        <span class="hljs-keyword">while</span> (l1 != <span class="hljs-keyword">null</span> &amp;&amp; l2 != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (l1.val &lt;= l2.val)&#123;
                pre.next = l1;
                l1 = l1.next;
            &#125; <span class="hljs-keyword">else</span> &#123;
                pre.next = l2;
                l2 = l2.next;
            &#125;
            pre = pre.next;
        &#125;
        pre.next = l1 != <span class="hljs-keyword">null</span> ? l1 : l2;
        <span class="hljs-keyword">return</span> res.next;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/merge-k-sorted-lists/">23-合并K个升序链表</a></p>
<p><code>两两合并</code></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeKLists</span><span class="hljs-params">(ListNode[] lists)</span> </span>&#123;
        <span class="hljs-keyword">int</span> n = lists.length;
        ListNode res = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++)&#123;
            <span class="hljs-comment">//两个两个合并</span>
            res = mergeTwoLists(res, lists[i]);
        &#125;
        <span class="hljs-keyword">return</span> res;
    &#125;
	
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeTwoLists</span><span class="hljs-params">(ListNode l1, ListNode l2)</span></span>&#123;
        ListNode res = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        ListNode pre = res;
        <span class="hljs-keyword">while</span> (l1 != <span class="hljs-keyword">null</span> &amp;&amp; l2 != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (l1.val &lt;= l2.val)&#123;
                pre.next = l1;
                l1 = l1.next;
            &#125; <span class="hljs-keyword">else</span> &#123;
                pre.next = l2;
                l2 = l2.next;
            &#125;
            pre = pre.next;
        &#125;
        pre.next = l1 != <span class="hljs-keyword">null</span> ? l1 : l2;
        <span class="hljs-keyword">return</span> res.next;
    &#125;
&#125;</code></pre>
<p><code>分治</code></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode() &#123;&#125;</span>
<span class="hljs-comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span>
<span class="hljs-comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeKLists</span><span class="hljs-params">(ListNode[] lists)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (lists == <span class="hljs-keyword">null</span> || lists.length == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">return</span> merge(lists, <span class="hljs-number">0</span>, lists.length - <span class="hljs-number">1</span>);
    &#125;

    <span class="hljs-comment">//分治</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">merge</span><span class="hljs-params">(ListNode[] lists, <span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> right)</span></span>&#123;
        <span class="hljs-keyword">if</span> (left == right) <span class="hljs-keyword">return</span> lists[left];
        <span class="hljs-keyword">if</span> (left &gt; right) <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">int</span> mid = (right + left) / <span class="hljs-number">2</span>;
        ListNode l1 = merge(lists, left, mid);
        ListNode l2 = merge(lists, mid + <span class="hljs-number">1</span>, right);
        <span class="hljs-keyword">return</span> mergeTwoLists(l1, l2);
    &#125;

    <span class="hljs-comment">//两个两个合并</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeTwoLists</span><span class="hljs-params">(ListNode l1, ListNode l2)</span></span>&#123;
        <span class="hljs-keyword">if</span> (l1 == <span class="hljs-keyword">null</span> || l2 == <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">return</span> l1 != <span class="hljs-keyword">null</span> ? l1 : l2;
        &#125;
        ListNode res = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        ListNode pre = res;
        <span class="hljs-keyword">while</span> (l1 != <span class="hljs-keyword">null</span> &amp;&amp; l2 != <span class="hljs-keyword">null</span>)&#123;
            <span class="hljs-keyword">if</span> (l1.val &lt;= l2.val)&#123;
                pre.next = l1;
                l1 = l1.next;
            &#125; <span class="hljs-keyword">else</span> &#123;
                pre.next = l2;
                l2 = l2.next;
            &#125;
            pre = pre.next;
        &#125;
        pre.next = l1 != <span class="hljs-keyword">null</span> ? l1 : l2;
        <span class="hljs-keyword">return</span> res.next;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/">剑指Offer 53-Ⅱ. 0 ~ n - 1中缺失的数字</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">missingNumber</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums)</span> </span>&#123;
        <span class="hljs-comment">// int n = nums.length;</span>
        <span class="hljs-comment">// int sum = 0;</span>
        <span class="hljs-comment">// for (int num : nums)&#123;</span>
        <span class="hljs-comment">//     sum += num;</span>
        <span class="hljs-comment">// &#125;</span>
        <span class="hljs-comment">// return n * (n + 1) / 2 - sum;</span>

         <span class="hljs-comment">//二分</span>
        <span class="hljs-keyword">int</span> left = <span class="hljs-number">0</span>, right = nums.length - <span class="hljs-number">1</span>;
        <span class="hljs-keyword">while</span> (left &lt;= right)&#123;
            <span class="hljs-keyword">int</span> mid = (right - left) / <span class="hljs-number">2</span> + left;
            <span class="hljs-keyword">if</span> (nums[mid] == mid)&#123;
                left = mid + <span class="hljs-number">1</span>;
            &#125; <span class="hljs-keyword">else</span> &#123;
                right = mid - <span class="hljs-number">1</span>;
            &#125;
        &#125;
        <span class="hljs-keyword">return</span> left;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/lru-cache/">146-LRU缓存机制</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LRUCache</span> </span>&#123;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> capacity = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">private</span> Map&lt;Integer, Integer&gt; map = <span class="hljs-keyword">new</span> LinkedHashMap&lt;&gt;();
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">LRUCache</span><span class="hljs-params">(<span class="hljs-keyword">int</span> capacity)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.capacity = capacity;
    &#125;
    
    <span class="hljs-comment">//get操作会访问key，先删除再添加，使其在链表末尾，保持最新访问</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">get</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (map.containsKey(key))&#123;
            <span class="hljs-keyword">int</span> val = map.get(key);
            map.remove(key);
            map.put(key, val);
            <span class="hljs-keyword">return</span> val;
        &#125; <span class="hljs-keyword">else</span> &#123;
            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
        &#125;
    &#125;
    
    <span class="hljs-comment">//put操作也会访问key，也是先删除再添加</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">put</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key, <span class="hljs-keyword">int</span> value)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (map.containsKey(key))&#123;
            map.remove(key);
        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (map.size() == capacity)&#123;
            <span class="hljs-comment">//移除表头结点，即最近最久未使用的结点</span>
            <span class="hljs-keyword">int</span> oldestKey = map.keySet().iterator().next();
            map.remove(oldestKey);
        &#125;
        map.put(key, value);

    &#125;
&#125;

<span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Your LRUCache object will be instantiated and called as such:</span>
<span class="hljs-comment"> * LRUCache obj = new LRUCache(capacity);</span>
<span class="hljs-comment"> * int param_1 = obj.get(key);</span>
<span class="hljs-comment"> * obj.put(key,value);</span>
<span class="hljs-comment"> */</span></code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/">剑指Offer 11.旋转数组的最小数字</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">minArray</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] numbers)</span> </span>&#123;
        <span class="hljs-keyword">int</span> n = numbers.length;
        <span class="hljs-keyword">int</span> left = <span class="hljs-number">0</span>, right = n - <span class="hljs-number">1</span>;
        <span class="hljs-keyword">while</span> (left &lt; right) &#123;
            <span class="hljs-keyword">int</span> mid = (left + right) / <span class="hljs-number">2</span>;
            <span class="hljs-keyword">if</span> (numbers[mid] &gt; numbers[right])&#123;
                left = mid + <span class="hljs-number">1</span>;
            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (numbers[mid] &lt; numbers[left])&#123;
                right = mid;
            &#125; <span class="hljs-keyword">else</span> &#123;
                right--;
            &#125;
        &#125;
        <span class="hljs-keyword">return</span> numbers[left];
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/multiply-strings/">43-字符串相乘</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">multiply</span><span class="hljs-params">(String num1, String num2)</span> </span>&#123;
        <span class="hljs-keyword">if</span> (num1.equals(<span class="hljs-string">&quot;0&quot;</span>) || num2.equals(<span class="hljs-string">&quot;0&quot;</span>)) &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;0&quot;</span>;
        &#125;
        <span class="hljs-keyword">int</span> n1 = num1.length(), n2 = num2.length();
        <span class="hljs-keyword">int</span>[] res = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[n1 + n2];
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = n1 - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--)&#123;
            <span class="hljs-keyword">int</span> a = num1.charAt(i) - <span class="hljs-string">&#x27;0&#x27;</span>;
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = n2 - <span class="hljs-number">1</span>; j &gt;= <span class="hljs-number">0</span>; j--)&#123;
                <span class="hljs-keyword">int</span> b = num2.charAt(j) - <span class="hljs-string">&#x27;0&#x27;</span>;
                <span class="hljs-comment">//该位累加和</span>
                <span class="hljs-keyword">int</span> sum = res[i + j + <span class="hljs-number">1</span>] + (a * b);
                <span class="hljs-comment">//该位的数</span>
                res[i + j + <span class="hljs-number">1</span>] = sum % <span class="hljs-number">10</span>;
                <span class="hljs-comment">//进位加到前一位</span>
                res[i + j] += sum / <span class="hljs-number">10</span>;
            &#125;
        &#125;
        StringBuilder sb = <span class="hljs-keyword">new</span> StringBuilder();
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; res.length; i++)&#123;
            <span class="hljs-comment">//如果首位是0，跳过它</span>
            <span class="hljs-keyword">if</span> (i == <span class="hljs-number">0</span> &amp;&amp; res[<span class="hljs-number">0</span>] == <span class="hljs-number">0</span>) <span class="hljs-keyword">continue</span>;
            sb.append(res[i]);
        &#125;
        <span class="hljs-keyword">return</span> sb.toString();
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/jump-game/">55-跳跃游戏</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">canJump</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums)</span> </span>&#123;
        <span class="hljs-keyword">int</span> farthest = <span class="hljs-number">0</span>;
        <span class="hljs-comment">//根据上一步能到达的最远位置，更新下一步能到达的最远位置</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;= farthest; i++)&#123;
            farthest = Math.max(farthest, i + nums[i]);
            <span class="hljs-comment">//能到达的最远位置比n-1大，说明能到达最后一个下标</span>
            <span class="hljs-keyword">if</span> (farthest &gt;= nums.length - <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/">剑指Offer 09.用两个栈实现队列</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CQueue</span> </span>&#123;
    Deque&lt;Integer&gt; stk1;
    Deque&lt;Integer&gt; stk2;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">CQueue</span><span class="hljs-params">()</span> </span>&#123;
        stk1 = <span class="hljs-keyword">new</span> ArrayDeque&lt;&gt;();
        stk2 = <span class="hljs-keyword">new</span> ArrayDeque&lt;&gt;();
    &#125;
    
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">appendTail</span><span class="hljs-params">(<span class="hljs-keyword">int</span> value)</span> </span>&#123;
        stk1.push(value);
    &#125;
    
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">deleteHead</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">if</span> (stk2.isEmpty())&#123;
            <span class="hljs-keyword">while</span> (!stk1.isEmpty())&#123;
                stk2.push(stk1.pop());
            &#125;
        &#125;
        <span class="hljs-comment">//队列为空</span>
        <span class="hljs-keyword">if</span> (stk2.isEmpty())&#123;
            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
        &#125; <span class="hljs-keyword">else</span> &#123;
            <span class="hljs-keyword">return</span> stk2.pop();
        &#125;
        
    &#125;
&#125;

<span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Your CQueue object will be instantiated and called as such:</span>
<span class="hljs-comment"> * CQueue obj = new CQueue();</span>
<span class="hljs-comment"> * obj.appendTail(value);</span>
<span class="hljs-comment"> * int param_2 = obj.deleteHead();</span>
<span class="hljs-comment"> */</span></code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/">剑指Offer 22.链表中倒数第K个节点</a></p>
<pre><code class="hljs java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * Definition for singly-linked list.</span>
<span class="hljs-comment"> * public class ListNode &#123;</span>
<span class="hljs-comment"> *     int val;</span>
<span class="hljs-comment"> *     ListNode next;</span>
<span class="hljs-comment"> *     ListNode(int x) &#123; val = x; &#125;</span>
<span class="hljs-comment"> * &#125;</span>
<span class="hljs-comment"> */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">getKthFromEnd</span><span class="hljs-params">(ListNode head, <span class="hljs-keyword">int</span> k)</span> </span>&#123;
        ListNode fastPtr = head, slowPtr = head;
        <span class="hljs-keyword">while</span> (k-- &gt; <span class="hljs-number">0</span>)&#123;
            fastPtr = fastPtr.next;
        &#125;
        <span class="hljs-keyword">while</span> (fastPtr != <span class="hljs-keyword">null</span>)&#123;
            fastPtr = fastPtr.next;
            slowPtr = slowPtr.next;
        &#125;
        <span class="hljs-keyword">return</span> slowPtr;
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/">剑指Offer 58-Ⅰ.翻转单词顺序</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">reverseWords</span><span class="hljs-params">(String s)</span> </span>&#123;
        StringBuilder sb = <span class="hljs-keyword">new</span> StringBuilder();
        String[] strs = s.trim().split(<span class="hljs-string">&quot; &quot;</span>);
        <span class="hljs-keyword">if</span> (strs.length == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> s;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = strs.length - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--)&#123;
            <span class="hljs-comment">//跳过空单词</span>
            <span class="hljs-keyword">if</span>(strs[i].equals(<span class="hljs-string">&quot;&quot;</span>)) <span class="hljs-keyword">continue</span>;
            <span class="hljs-comment">//最后一个不需要+空格</span>
            sb.append(strs[i] + <span class="hljs-string">&quot; &quot;</span>);
        &#125;
        <span class="hljs-comment">//去除首尾的空格</span>
        <span class="hljs-keyword">return</span> sb.toString().trim();
    &#125;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/">1047-删除字符串中的所有相邻重复项</a></p>
<pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">removeDuplicates</span><span class="hljs-params">(String S)</span> </span>&#123;
        Deque&lt;Character&gt; stk = <span class="hljs-keyword">new</span> ArrayDeque&lt;&gt;();
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">char</span> c : S.toCharArray())&#123;
            <span class="hljs-keyword">if</span> (!stk.isEmpty() &amp;&amp; stk.peek().equals(c))&#123;
                stk.pop();
            &#125; <span class="hljs-keyword">else</span> &#123;
                stk.push(c);
            &#125;
        &#125;

        StringBuilder sb = <span class="hljs-keyword">new</span> StringBuilder();
        <span class="hljs-keyword">while</span> (!stk.isEmpty())&#123;
            sb.insert(<span class="hljs-number">0</span>, stk.pop());
        &#125;
         <span class="hljs-keyword">return</span> sb.toString();
    &#125;
&#125;</code></pre>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E7%AE%97%E6%B3%95/">算法</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E7%AC%94%E8%AF%95%E9%A2%98/">笔试题</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/03/%E6%89%8B%E6%92%95%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">手撕排序算法</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/05/03/Netty%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A0/">
                        <span class="hidden-mobile">Netty基础学习</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "JD笔试&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.staticfile.org/mathjax/3.0.5/es5/tex-svg.js" ></script>

  











</body>
</html>
